package WordCount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Map;
import java.util.Properties;

public class HDFSWCReflect {

    public static void main(String[] args) throws Exception{

        Properties properties = ParamUtils.getProperties();

        FileSystem fs = FileSystem.get(new URI(properties.get(Constants.HDFS_URI).toString()  ),new Configuration());
        Path path = new Path(properties.get(Constants.INPUT_PATH).toString());
        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(path,false);

        BaseContext baseContext = new BaseContext();

        // 通过反射动态获取Mapper类
        Class<?>clazz = Class.forName(properties.get(Constants.MAPPER_CLASS).toString());
        BaseMapper baseMapper = (BaseMapper) clazz.newInstance();

        while (iterator.hasNext()){
            LocatedFileStatus file = iterator.next();
            FSDataInputStream in = fs.open(file.getPath());
            // 装饰者模式
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null){
                baseMapper.map(line,baseContext);
            }
            reader.close();
            in.close();
        }


        Path target = new Path(properties.get(Constants.OUTPUT_PATH).toString());
        FSDataOutputStream out = fs.create(new Path(target,new Path(properties.get(Constants.OUTPUT_FILE).toString())));
        Map<String,Integer> contextMap = baseContext.getCacheMap();
        for (Map.Entry<String,Integer> entry : contextMap.entrySet()){
            out.write((entry.getKey() + "\t" + entry.getValue() + "\n").getBytes());
        }
        out.close();
        fs.close();

        System.out.println("执行完成。。。。");

    }


    // 反射获取类信息
    public static void reflectTest() throws Exception{

        Class<?>claszz = Class.forName("WordCount.SmallAnimal");
        Things things =  (Things) claszz.newInstance();
        System.out.println(things.getClass().getSimpleName());
    }
}


interface Things{

}

class Animal implements Things{

    public Animal(){
        System.out.println("动物");
    }
}

class SmallAnimal extends Animal{

    public SmallAnimal(){
        System.out.println("小动物");
    }
}

class Dog extends SmallAnimal{

    public Dog(){
        System.out.println("狗");
    }
}